package com.talent.jiwen.ui.widgets;

import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;
import android.widget.TextView;

import com.talent.jiwen.R;
import com.talent.jiwen.training.data.IdNameBean;
import com.zhy.adapter.recyclerview.CommonAdapter;
import com.zhy.adapter.recyclerview.base.ViewHolder;

import java.util.List;

/**
 * Created by Jason on 2018/5/21.
 */

public class TrainingSelectLayoutPop extends SupportPopWindow {
    private TextView titleTv;
    private RecyclerView recyclerView;
    private Context context;

    public TrainingSelectLayoutPop(Context context) {
        super(context);
        this.context = context;
        View view = LayoutInflater.from(context).inflate(R.layout.layout_training_select_pop , null);

        titleTv = (TextView)view.findViewById(R.id.titleTv);
        recyclerView = (RecyclerView)view.findViewById(R.id.recycleView);

        //设置View
        setContentView(view);


        //设置宽与高
        setWidth(WindowManager.LayoutParams.MATCH_PARENT);

        setHeight(WindowManager.LayoutParams.WRAP_CONTENT);


        /**
         * 设置背景只有设置了这个才可以点击外边和BACK消失
         */
        setBackgroundDrawable(new ColorDrawable());


        /**
         * 设置可以获取集点
         */
        setFocusable(true);

        /**
         * 设置点击外边可以消失
         */
        setOutsideTouchable(true);

        /**
         *设置可以触摸
         */
        setTouchable(true);


        /**
         * 设置点击外部可以消失
         */

        setTouchInterceptor(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {

                /**
                 * 判断是不是点击了外部
                 */
                if(event.getAction()==MotionEvent.ACTION_OUTSIDE){
                    return true;
                }
                //不是点击外部
                return false;
            }
        });



    }

    /**设置数据
     *
     * @param title
     * @param dataList
     * @param onTikuSelectItemClickListener
     */
    public void setData(String title , List<IdNameBean> dataList , final OnTikuSelectItemClickListener onTikuSelectItemClickListener){
        titleTv.setText(title);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(context);
        linearLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
        recyclerView.setLayoutManager(linearLayoutManager);

        recyclerView.setAdapter(new CommonAdapter<IdNameBean>(context , R.layout.item_training_select , dataList){
            @Override
            protected void convert(ViewHolder holder, final IdNameBean idNameBean, int position) {
                TextView contentTv = holder.getView(R.id.contentTv);

                if (idNameBean.isSelected()){
                    contentTv.setTextColor(ContextCompat.getColor(mContext , R.color.white));
                    contentTv.setBackgroundResource(R.drawable.bg_circle_theme);
                }else{
                    contentTv.setTextColor(ContextCompat.getColor(mContext , R.color.common_app_color));
                    contentTv.setBackgroundResource(R.drawable.bg_empty_circle_blue);
                }

                contentTv.setText(idNameBean.getName());

                holder.itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        onTikuSelectItemClickListener.itemClick(idNameBean);
                    }
                });
            }
        });
    }

    public interface OnTikuSelectItemClickListener{
        void itemClick(IdNameBean idNameBean);
    }

}
